package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysRoleMenuService;
import com.atguigu.spzx.model.pojo.system.SysMenu;
import com.atguigu.spzx.model.pojo.system.SysRoleMenu;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 * @description 针对表【sys_role_menu(角色菜单)】的数据库操作Service实现
 * @createDate 2025-01-03 18:11:03
 */
@Service
@Transactional
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
        implements SysRoleMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findMenuByRoleId(Long id) {
        return sysRoleMenuMapper.findMenuByRoleId(id);
    }

    @Override
    public boolean updateMenuByRoleId(Long id, List<Long> menuIds) {
        // 先删除原有的
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id", id));
        // 再添加
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setIsHalf(0);
            sysRoleMenu.setRoleId(id);
            sysRoleMenus.add(sysRoleMenu);
        }
        sysRoleMenuMapper.insert(sysRoleMenus);
        return true;
    }
}




